// var websock = new WebSocket("ws://120.78.209.158:8080/chat");
const wsUrl='ws://127.0.0.1:8088/ws'
let websock=''
let socket={
  init(){
    this.WS=new WebSocket(wsUrl);
  },

}





export default {
  initWebSocket:function(){
    websock = new WebSocket(wsUrl);
    websock.onopen = function() {
      console.log("连接建立成功...");
      var dataContent = {
        action : 1,
        chatMsg: {
          sendUserId : 'f3aecf2877ed40738971f95a5481069b',
          acceptUserId : '',
          msg : '建立链接时候发送数据，在后台将发送者的 id 和 channel 绑定起来',
          msgId : '111',
        },
        extand : null,
      }
      console.log("ws:"+websock);
      websock.send(JSON.stringify(dataContent));
      //建立连接之后获取用户未读消息
      //TODO

      //定时发送心跳检测 10s
      setInterval(this.keepalive,10000);
    };
    websock.onmessage = this.onmessage();
    websock.onerror = this.onerror();
    websock.onclose = this.onclose();
  },
  onopen: function() {
    console.log("连接建立成功...");
    var dataContent = {
      action : 1,
      chatMsg: {
        sendUserId : 'f3aecf2877ed40738971f95a5481069b',
        acceptUserId : '',
        msg : '建立链接时候发送数据，在后台将发送者的 id 和 channel 绑定起来',
        msgId : '111',
      },
      extand : null,
    }
    console.log("ws:"+websock);
    websock.send(JSON.stringify(dataContent));
    //建立连接之后获取用户未读消息
    //TODO

    //定时发送心跳检测 10s
      setInterval(this.keepalive,10000);
  },
  send:function(Data){//数据发送
   this.websock.send(Data);
  },
  onclose: function() {
    clearInterval(heartCheck);
    console.log("连接关闭...");
  },
  onerror: function() {
    clearInterval(heartCheck);
    console.log("发生错误...");
  },
  onmessage: function(e) {
    console.log("接受到消息：" + e.data);
    var receiveMsg = document.getElementById("receiveMsg");
    var html = receiveMsg.innerHTML;
    receiveMsg.innerHTML = html + "<br/>" + e.data;
  },
  keepalive: function(){
    console.log("看看有没有执行心跳检测")
    //发送心跳检测
    var dataContent = {
      action : 4,
      chatMsg: {
        sendUserId : '',
        acceptUserId : '',
        msg : '发送心跳',
        msgId : '',
      },
      extand : null,
    }
    websock.send(JSON.stringify(dataContent));
  }


}
